<template>
    <div class="icon-select">
        <el-popover placement="bottom" :width="500" trigger="click" v-model:visible="visible">
            <template #reference>
                <el-input 
                    v-model="currentIcon" 
                    placeholder="点击选择图标" 
                    readonly
                    :suffix-icon="currentIcon"
                />
            </template>
            
            <div class="icon-list">
                <div class="search-input">
                    <el-input 
                        v-model="searchText" 
                        placeholder="搜索图标" 
                        clearable
                        prefix-icon="Search"
                    />
                </div>
                
                <el-scrollbar height="300px">
                    <div class="icon-grid">
                        <div 
                            v-for="icon in filteredIcons" 
                            :key="icon"
                            class="icon-item"
                            :class="{ active: currentIcon === icon }"
                            @click="selectIcon(icon)"
                        >
                            <el-icon>
                                <component :is="icon" />
                            </el-icon>
                            <span class="icon-name">{{ icon }}</span>
                        </div>
                    </div>
                </el-scrollbar>
            </div>
        </el-popover>
    </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const props = defineProps({
    modelValue: {
        type: String,
        default: ''
    }
})

const emit = defineEmits(['update:modelValue'])

// 弹窗显示控制
const visible = ref(false)

// 搜索文本
const searchText = ref('')

// 当前选中的图标
const currentIcon = computed({
    get: () => props.modelValue,
    set: (val) => emit('update:modelValue', val)
})

// 所有图标列表
const iconList = Object.keys(ElementPlusIconsVue)

// 过滤后的图标列表
const filteredIcons = computed(() => {
    if (!searchText.value) return iconList
    return iconList.filter(icon => 
        icon.toLowerCase().includes(searchText.value.toLowerCase())
    )
})

// 选择图标
const selectIcon = (icon: string) => {
    currentIcon.value = icon
    visible.value = false
}
</script>

<style scoped lang="scss">
.icon-select {
    .icon-list {
        .search-input {
            margin-bottom: 15px;
        }

        .icon-grid {
            display: grid;
            grid-template-columns: repeat(8, 1fr);
            gap: 12px;
            padding: 8px;

            .icon-item {
                display: flex;
                flex-direction: column;
                align-items: center;
                padding: 8px;
                cursor: pointer;
                border-radius: 4px;
                transition: all 0.3s;

                &:hover {
                    background-color: var(--el-color-primary-light-9);
                    color: var(--el-color-primary);
                }

                &.active {
                    background-color: var(--el-color-primary);
                    color: white;
                }

                .el-icon {
                    font-size: 20px;
                    margin-bottom: 4px;
                }

                .icon-name {
                    font-size: 12px;
                    text-align: center;
                    word-break: break-all;
                }
            }
        }
    }
}
</style> 